package com.zkt.auth.application.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.auth.application.convert.AuthUserDTOConverter;
import com.zkt.auth.entity.AuthUserDTO;
import com.zkt.auth.common.entity.Result;
import com.zkt.auth.domain.entity.AuthUserBO;
import com.zkt.auth.domain.service.AuthUserDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/22 13:24
 * @description 用户相关controller层
 **/

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
	
	@Resource
	private AuthUserDomainService authUserDomainService;
	
	/**
	 * 用户登录
	 *
	 * @param authUserDTO 用户登录信息
	 * @return 是否成功
	 */
	@PostMapping("login")
	public Result<SaTokenInfo> login(@RequestBody AuthUserDTO authUserDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.login.dto:{}", JSON.toJSONString(authUserDTO));
			}
			
			checkUserInfo(authUserDTO);
			
			AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBo(authUserDTO);
			
			return Result.ok(authUserDomainService.login(authUserBO));
		} catch (Exception e) {
			log.error("UserController.login.error:{}", e.getMessage(), e);
			return Result.fail("登录失败");
		}
	}
	
	/**
	 * 用户验证码登录
	 *
	 * @param captcha 验证码
	 * @return 鉴权信息
	 */
	@GetMapping("login")
	public Result<SaTokenInfo> loginByCaptcha(@RequestParam("captcha") String captcha) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.loginByCaptcha.captcha:{}", JSON.toJSONString(captcha));
			}
			
			if (StrUtil.isBlank(captcha)) {
				throw new Exception("验证码不能为空");
			}
			
			SaTokenInfo tokenInfo = authUserDomainService.loginByCaptcha(captcha);
			
			return Result.ok(tokenInfo);
		} catch (Exception e) {
			log.error("UserController.loginByCaptcha.error:{}", e.getMessage(), e);
			return Result.fail("登录失败");
		}
	}
	
	/**
	 * 用户退出登录
	 *
	 * @return 是否成功
	 */
	@GetMapping("logout")
	public Result<Boolean> logout() {
		try {
			return Result.ok(authUserDomainService.logout());
		} catch (Exception e) {
			log.error("UserController.logout.error:{}", e.getMessage(), e);
			return Result.fail("用户退出登录失败");
		}
	}
	
	/**
	 * 用户注册
	 *
	 * @param authUserDTO 注册信息
	 * @return 是否成功
	 */
	@PostMapping("register")
	public Result<Boolean> register(@RequestBody AuthUserDTO authUserDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.register.dto:{}", JSON.toJSONString(authUserDTO));
			}
			
			checkUserInfo(authUserDTO);
			// if (StrUtil.isBlank(authUserDTO.getEmail())) {
			// 	throw new Exception("邮箱不能为空");
			// }
			
			AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBo(authUserDTO);
			
			return Result.ok(authUserDomainService.register(authUserBO));
		} catch (Exception e) {
			log.error("UserController.register.error:{}", e.getMessage(), e);
			return Result.fail("注册用户失败");
		}
	}
	
	/**
	 * 修改用户信息
	 *
	 * @param authUserDTO 修改后的用户信息
	 * @return 是否成功
	 */
	@PostMapping("update")
	public Result<Boolean> update(@RequestBody AuthUserDTO authUserDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.update.dto:{}", JSON.toJSONString(authUserDTO));
			}
			
			AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBo(authUserDTO);
			
			return Result.ok(authUserDomainService.update(authUserBO));
		} catch (Exception e) {
			log.error("UserController.update.error:{}", e.getMessage(), e);
			return Result.fail("更新用户信息失败");
		}
	}
	
	/**
	 * 删除用户
	 *
	 * @param authUserDTO 需要删除的用户信息
	 * @return 是否成功
	 */
	@PostMapping("delete")
	public Result<Boolean> delete(@RequestBody AuthUserDTO authUserDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.delete.dto:{}", JSON.toJSONString(authUserDTO));
			}
			
			if (ObjUtil.isEmpty(authUserDTO.getId())) {
				throw new Exception("用户id不能为空");
			}
			
			AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBo(authUserDTO);
			
			return Result.ok(authUserDomainService.delete(authUserBO));
		} catch (Exception e) {
			log.error("UserController.delete.error:{}", e.getMessage(), e);
			return Result.fail("删除用户失败");
		}
	}
	
	/**
	 * 用户启用/禁用
	 *
	 * @param authUserDTO 用户信息
	 * @return 是否成功
	 */
	@PostMapping("changeStatus")
	public Result<Boolean> changeStatus(@RequestBody AuthUserDTO authUserDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(authUserDTO));
			}
			
			if (ObjUtil.isEmpty(authUserDTO.getId())) {
				throw new Exception("用户id不能为空");
			}
			if (ObjUtil.isEmpty(authUserDTO.getStatus())) {
				throw new Exception("用户状态不能为空");
			}
			
			AuthUserBO authUserBO = AuthUserDTOConverter.INSTANCE.convertDtoToBo(authUserDTO);
			
			return Result.ok(authUserDomainService.changeStatus(authUserBO));
		} catch (Exception e) {
			log.error("UserController.changeStatus.error:{}", e.getMessage(), e);
			return Result.fail("用户启用/禁用失败");
		}
	}
	
	/**
	 * 获取用户信息
	 *
	 * @return 用户信息
	 */
	@GetMapping("getUserInfo")
	public Result<AuthUserDTO> getUserInfo(@RequestParam(value = "username", required = false) String username) {
		try {
			if (log.isInfoEnabled()) {
				log.info("UserController.getUserInfo.username:{}", username);
			}
			
			AuthUserBO authUserBO = authUserDomainService.getUserInfo(username);
			return Result.ok(AuthUserDTOConverter.INSTANCE.convertBoToDto(authUserBO));
		} catch (Exception e) {
			log.error("UserController.getUserInfo.error:{}", e.getMessage(), e);
			return Result.fail("获取用户信息失败");
		}
	}
	
	private static void checkUserInfo(AuthUserDTO authUserDTO) throws Exception {
		if (StrUtil.isBlank(authUserDTO.getUserName())) {
			throw new Exception("用户名不能为空");
		}
		if (StrUtil.isBlank(authUserDTO.getPassword())) {
			throw new Exception("密码不能为空");
		}
	}
	
}